(function (p) { /* Sai.Calendar.Agenda.Menu */

    var Menu = function (container) {

        this.container      = container;
        this.showAll        = container.find("button.show-all");
        this.hideAll        = container.find("button.hide-all");
        this.from           = container.find("input[name=from]");
        this.to             = container.find("input[name=to]");
        this.applyFilter    = container.find("button.apply-filter");
        this.clearFilter    = container.find("button.clear-filter");
        
        this.from.datepicker();
        this.to.datepicker();

        this.showAll.click($.proxy(this.onShowAllButtonClick, this));
        this.hideAll.click($.proxy(this.onHideAllButtonClick, this));
        this.applyFilter.click($.proxy(this.onApplyFilterButtonClick, this));
        this.clearFilter.click($.proxy(this.onClearFilterButtonClick, this));
        
    }
    Menu.fn = Menu.prototype;


    EventDispatcher.prototype.apply(Menu.prototype);


    Menu.fn.clear = function ( clearInputs ) {
        
        this.from.removeAttr("disabled", "true");
        this.to.removeAttr("disabled", "true");
        
        if(clearInputs){
            this.from.val("");
            this.to.val("");
        }
        
        this.clearFilter.hide();
        this.applyFilter.show();
        
        this.filtering = false;
        
    }


    Menu.fn.filter = function ( from, to ) {
        
        //console.log("Menu.fn.filter - from:"+from+" - to:"+to);
        this.from.attr("disabled", "true");
        this.to.attr("disabled", "true");
        
        this.from.val(from);
        this.to.val(to);
        
        this.applyFilter.hide();
        this.clearFilter.show();
        
        this.filtering = true;
        
    }


    Menu.fn.onShowAllButtonClick = function ( event ) {

        this.dispatchEvent({
            type: Menu.ON_SHOW_ALL
        });

    }


    Menu.fn.onHideAllButtonClick = function ( event ) {

        this.dispatchEvent({
            type: Menu.ON_HIDE_ALL
        });

    }


    Menu.fn.onClearFilterButtonClick = function ( event ) {
        
        this.clear();
        this.dispatchEvent({
            type: Menu.ON_CLEAR_FILTER
        });

    }


    Menu.fn.onApplyFilterButtonClick = function ( event ) {
        
        if(!this.from.val()){
            this.from.focus();
            return
        }
        
        if(!this.to.val()){
            this.to.focus();
            return
        }
        
        var from    = new Date(this.from.val());
        var to      = new Date(this.to.val());
        
        if(!from.getTime()){
            this.from.focus();
            alert("Fecha mal escrita");
            return
        }
        
        if(!to.getTime()){
            this.to.focus();
            alert("Fecha mal escrita");
            return
        }
        
        this.dispatchEvent({
            type: Menu.ON_APPLY_FILTER,
            from: this.from.val(),
            to: this.to.val()
        });

    }

    Menu.ON_SHOW_ALL        = "onShowAll";
    Menu.ON_HIDE_ALL        = "onHideAll";
    Menu.ON_APPLY_FILTER    = "onApplyFilter";
    Menu.ON_CLEAR_FILTER    = "onClearFilter";

    p.Menu = Menu;

})(Sai.Calendar.Agenda);